package com.kw.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kw.pojo.JwtPayLoad;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.time.LocalTime;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

/**
 * Created by Medivh on 2020/7/3.
 * 愿我出走半生,归来仍是少年
 */
public class JwtUtils {

      /**
     * 非对称加密token

     * @param privateKey 私钥
     * @param expire     过期时间，单位秒
     * @return JWT
     */
    public static String generateTokenRSA(JwtPayLoad jwtPayLoad, PrivateKey privateKey, int expire) {
        final Date expirationDate = new Date(System.currentTimeMillis() + expire * 1000);
        return Jwts.builder()
                .setSubject(String.valueOf(jwtPayLoad.getUserId()))
                .setClaims(jwtPayLoad.toMap())
                .setExpiration(expirationDate)
                .signWith(SignatureAlgorithm.RS256,privateKey)
                .compact();
    }

    public static JwtPayLoad getJwtPayLoadRSA(String token,PublicKey publicKey) {
        Claims claimFromToken = Jwts.parser().setSigningKey(publicKey).parseClaimsJws(token).getBody();;
        return JwtPayLoad.toBean(claimFromToken);
    }

    /**
     * 对称加密token


     * @param expire     过期时间，单位秒
     * @return JWT
     */
    public static String generateTokenDES(JwtPayLoad jwtPayLoad, String desKey, int expire) {
        final Date expirationDate = new Date(System.currentTimeMillis() + expire * 1000);
        return Jwts.builder()
                .setClaims(jwtPayLoad.toMap())
                .setSubject(String.valueOf(jwtPayLoad.getUserId()))
                .setExpiration(expirationDate)
                .signWith(SignatureAlgorithm.HS512,desKey)
                .compact();
    }


    public static JwtPayLoad getJwtPayLoadDES(String token,String  desKey) {
        //  Claims claimFromToken = getClaimFromToken(token);
        Claims claimFromToken = Jwts.parser().setSigningKey(desKey).parseClaimsJws(token).getBody();;
        return JwtPayLoad.toBean(claimFromToken);

        // return JwtPayLoad.toBean(claimFromToken);
    }


    


    public static String privateKey = "MIICd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IDAQAB";
    public static String desKey = "1234";






    public static void main(String args[]) throws Exception {
        JwtPayLoad payload = new JwtPayLoad();
        payload.setUserId(1L);
        payload.setAccount("123");
        payload.setUserKey("1232323");
        String jwt = JwtUtils.generateTokenRSA(payload,RSAUtil.getPrivateKey(privateKey),1000);
        System.out.println("jwt="+jwt);
        JwtPayLoad jwtPayLoad = JwtUtils.getJwtPayLoadRSA(jwt,RSAUtil.getPublicKey(publicKey));
        System.out.println("jwtPayLoad="+jwtPayLoad);


        String jwtDES = JwtUtils.generateTokenDES(payload,desKey,1000);
        System.out.println("jwt="+jwtDES);

        JwtPayLoad jwtPayLoadDes = JwtUtils.getJwtPayLoadDES(jwtDES,desKey);
        System.out.println(jwtPayLoadDes);

    }


















}
